﻿#include "loadmvbportcache.h"
#include<qdebug.h>
LoadMVBPortCache::LoadMVBPortCache()
{

}

bool LoadMVBPortCache::LoadMVBPort(const QString &fileName, MVBProtocalCache &protocalCache)
{
    bool bOpen =OpenXLSXFile(fileName);
    if(!bOpen)
    {
        qDebug()<<"打开文件失败:"<<fileName<<endl;
            return false;
    }

    //1.读取端口

    //设置当前表名
    bool bOK=SetCurrentSheetName(m_portSheetName);
    if(!bOK)
    {
        qDebug()<<"文件"<<fileName<<"未包含表名:"<<m_portSheetName<<endl;
            return false;
    }

    int rows =_xWorkSheet->dimension().lastRow();
    for (int row = 2; row < rows; ++row)
    {
        mvbDeviceData data;
        data.m_portAddress= _xWorkSheet->read(row,portCacheReadIndex::PortAddress).toString();
        data.m_size= _xWorkSheet->read(row,portCacheReadIndex::Size).toInt();
        data.m_circleTime= _xWorkSheet->read(row,portCacheReadIndex::CycleTime).toInt();
        data.m_portType= _xWorkSheet->read(row,portCacheReadIndex::PortType).toInt();
        if(data.m_portType==0||data.m_portType==1)
        {
            protocalCache.m_portCache[data.m_portAddress]=data;
        }

    }

    //2.读取变量
    //设置当前表名
    bool bSetOK=SetCurrentSheetName(m_SignalSheetName);
    if(!bSetOK)
    {
        qDebug()<<"文件"<<fileName<<"未包含表名:"<<m_portSheetName<<endl;
            return false;

    }
    int signalRows =_xWorkSheet->dimension().lastRow();

    //按端口号进行数据缓存
    MVBPortData data;
    for (int row = 2; row < signalRows; ++row)
    {
            QString portAddress =_xWorkSheet->read(row,signalsReadIndex::signal_PortAddress).toString();
                //端口列表中未包含则继续
            if(!protocalCache.m_portCache.contains(portAddress))
            {
                continue;
            }
            if(!protocalCache.m_portDataPars.contains(portAddress))
            {
                protocalCache.m_portDataPars[portAddress]=data;
            }

            //数据添加
            MVBVarCell varCell;
            varCell.m_MVBtype =  _xWorkSheet->read(row,signalsReadIndex::MVBType).toString();
            varCell.m_byteIndex =  _xWorkSheet->read(row,signalsReadIndex::ByteIndex).toInt();
            varCell.m_bitIndex =  _xWorkSheet->read(row,signalsReadIndex::BitIndex).toInt();
            varCell.m_bitSize =  _xWorkSheet->read(row,signalsReadIndex::bitSize).toInt();
            varCell.m_VarName =  _xWorkSheet->read(row,signalsReadIndex::VarName).toString();
            if(protocalCache.m_portDataPars.contains(portAddress))
            {
            protocalCache.m_portDataPars[portAddress].m_portVars.push_back(varCell);
            }


    }

    return true;
}

